Skip to content

fix(workflow): do not re-persist fast-forwarded outputs on resume - #8119

Draft
adimalkar wants to merge 1 commit into
google:mainfrom
adimalkar:fix/8058-no-reemit-persisted-replay-output
Draft

adimalkar wants to merge 1 commit into
google:mainfrom
adimalkar:fix/8058-no-reemit-persisted-replay-output

Conversation

@adimalkar

Copy link
Copy Markdown

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:

On a resumable Workflow, each resume fast-forwards the runs that finished before it, and Workflow._handle_completion calls _maybe_reemit_replayed_output for every one of them. If a run's output was recovered from the session, it is already persisted, so the re-emit stores it again on every later resume of the same invocation. A workflow that parks N times stores its outputs 1 + 2 + ... + N times, and the session grows quadratically.

Solution:

The re-emit covers two kinds of fast-forwarded run, and only one of them needs it:

  • An answered RequestInput (case 5 in check_interception). This resume builds the output from the resolved response, so the session does not hold it yet. The re-emit is what persists it, and test_workflow_request_input_resume[True] depends on that.
  • A run that finished in an earlier turn (case 4). Its output comes from recovered.output, so it is already in the session.

_handle_completion now re-emits only when run.recovered_state has no recovered output:

elif run.recovered_state is None or run.recovered_state.output is None:
  await self._maybe_reemit_replayed_output(child_ctx, ctx)

Once the re-emit has persisted an answered park's output, later resumes recover it as case 4 and skip it.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.

  • All unit tests pass locally.

  • New test_resume_does_not_re_persist_fast_forwarded_outputs in tests/unittests/workflow/test_workflow_hitl.py, based on the loop from Resumable Workflow re-persists every fast-forwarded output on each resume: session grows quadratically with the number of resumes #8058 with 4 rounds:

    • Asserts each answered park's output is stored once.
    • Asserts every resume except the last adds the same number of events.
    • On main it fails with 10 stored outputs instead of 4 (ask@1 four times, ask@2 three times, and so on).
  • test_workflow_parallel_worker.py changes:

    • test_parallel_worker_pauses_for_human_input and test_parallel_worker_hitl_respects_parallel_workers_limits expected NodeA@1's run-1 output to be emitted again on each resume. That is the duplicate this fixes, so I removed it from their run-2/run-3 expectations.
    • The new outputs those runs produce are still asserted.
  • Results on this branch, rebased on main 5ce1d0c, Python 3.11:

    • pytest tests/unittests/workflow: 1451 passed, 2 xfailed.
    • pytest tests/unittests: 24793 passed, 94 skipped, 39 xfailed, 2 xpassed, 0 failed.
    • pre-commit run --from-ref origin/main --to-ref HEAD: all hooks pass.
  • test_workflow_request_input_resume[True] fails if the re-emit is removed entirely. It passes with this change, so an answered park's output is still persisted.

Manual End-to-End (E2E) Tests:

Run the repro.py script from #8058 (plain google-adk, InMemorySessionService, no model) with python repro.py 10:

Before (main):

google-adk 2.14.1, 10 rounds
  events added per resume: [11, 12, 13, 14, 15, 16, 17, 18, 19, 18]
  session events: 160
  stored outputs per answered park: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

After (this branch):

google-adk 2.14.1, 10 rounds
  events added per resume: [11, 11, 11, 11, 11, 11, 11, 11, 11, 9]
  session events: 115
  stored outputs per answered park: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

With 100 rounds, the session ends at 1,105 events instead of 6,055, and the script runs in 6.9 s instead of 21 s.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

The 100-round numbers come from the pre-rebase branch at 59b0dd6. The 10-round output above was rerun after rebasing on 5ce1d0c.

This change was developed with AI assistance (Claude Code). The tests and the end-to-end runs above were done locally on Linux with Python 3.11.

On a resumable Workflow, every resume fast-forwards the runs that finished
before it, and _handle_completion re-emitted the output of each one. A run
whose output was recovered from the session was therefore stored again on
every later resume of the same invocation, so a workflow that parks N times
stored its outputs 1 + 2 + ... + N times.

Re-emit only an output that the session does not hold yet: one this resume
derived from resolved interrupts, as for an answered RequestInput. An output
recovered from the session is already persisted.

The parallel worker tests expected NodeA's run-1 output to be emitted again
on each resume; drop those duplicates from their expectations.

Fixes google#8058
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Resumable Workflow re-persists every fast-forwarded output on each resume: session grows quadratically with the number of resumes

2 participants